﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CSCSN.EntitiyModels;
using CSCSN.ORM;
using Microsoft.AspNetCore.Mvc;

namespace CSCSN.WebApplication.Controllers
{
    public class MissionController : Controller
    {
        private readonly CsDbContext db;
        public MissionController(CsDbContext assignment)

        {
            db = assignment;
        }
        public IActionResult Index()
        {
            var assignment = db.ApplicationUsers.Where(a => a.UserName == User.Identity.Name).FirstOrDefault();
            var item = db.ApplicationUserWithApplicationTasks.Where(s => s.ApplicationUsers == assignment).Select(s => s.ApplicationTasks).ToList();
            List<ApplicationTask> cb = new List<ApplicationTask>();
            return View(item);
        }
        public IActionResult Delete(Guid? id)
        {
            var assignment = db.ApplicationUsers.Where(a => a.UserName == User.Identity.Name).FirstOrDefault();
            var item = db.ApplicationUserWithApplicationTasks.Where(s => s.ApplicationUsers == assignment&&s.ApplicationTasks.Id==id).Select(s=>s.ApplicationTasks).FirstOrDefault();
            
            return View(item);
        }
        [HttpGet]
        public IActionResult Delete(Guid id)
        {
            var assignment = db.ApplicationUsers.Where(a => a.UserName == User.Identity.Name).FirstOrDefault();
            var item = db.ApplicationUserWithApplicationTasks.Where(s => s.ApplicationUsers == assignment && s.ApplicationTasks.Id == id).FirstOrDefault();
            db.Remove(item);
            db.SaveChanges();

            return RedirectToAction("Index", "Mission");
        }

    }
}